package cn.dubito.o2o.dao.spilt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liliang
 * @description:
 * @date 2018/4/23.
 */
public class DynamicDataSourceHolder {

    private static final Logger log = LoggerFactory.getLogger(DynamicDataSourceHolder.class);

    private static ThreadLocal<String> contextHolder = new ThreadLocal<>();

    public static final String DB_MASTER = "master";
    public static final String DB_SLAVE = "slave";

    /**
     * 获取数据源类型
     *
     * @return 类型
     */
    public static String getDbType() {
        String db = contextHolder.get();
        if (db == null) {
            db = DB_MASTER;
        }
        return db;
    }

    /**
     * 设置数据源类型
     *
     * @param dbType 类型
     */
    public static void setDbType(String dbType) {
        log.debug("所使用的数据源为:{}", dbType);
        contextHolder.set(dbType);
    }

    /**
     * 清空数据源类型
     */
    public static void clearDbType() {
        contextHolder.remove();
    }
}
